<?php

namespace Admin\Controller;

use Think\Controller;

class CasebController extends ExtendController {

    //显示栏目
    public function index(){
     $list = D('Case')->alias('a')->join(' left join ki_case_type as b on a.t_id =b.t_id')->order('a.sort asc')->select();
     if(!empty($list)){
            $this->assign('list',$list);  
     }
     $this->display('index'); // 输出模板
    }
   
   public function insert(){
     $list = D('CaseType')->select();
     $this->assign('list',$list);
     $this->display();
   }
   public function add(){
        $upload = new \Think\Upload();// 实例化上传类
      $upload->maxSize = 10485760;
      $upload->rootPath = './Public/images/';
      $upload->savePath = '';
      $upload->saveName = array('uniqid','');
      $upload->exts     = array('jpg', 'gif', 'png', 'jpeg');
      $upload->autoSub  = true;
      $upload->subName  = array('date','Ymd');
       // 上传文件 
      $info   =   $upload->upload($_FILES);
      if(!$info) {// 上传错误提示错误信息
         $this->error($upload->getError());
      }else{
         foreach($info as $file){
             $info= $file['savepath'].$file['savename'];
             $data['img_url'] = $info;
       } 
      } 
    $data['content']=$_POST['message'];
    $data['t_id']=$_POST['type'];
    $data['title']=$_POST['title'];
    $data['sort'] = $_POST['sort'];
    $data['createtime'] = date('Y-m-d H:i:s');
    
     $result = D('Case')->add($data);
            if ($result != false) {
            $this->redirect('Caseb/index');
        } else {
            $this->error('删除失败!');
        }

   }


         //修改
     public function update() {
        if(IS_POST){
                $where['id'] = $_POST['id'];
                    $data['content']=$_POST['message'];
                    $data['t_id']=$_POST['type'];
                    $data['title']=$_POST['title'];
                    $data['sort'] = $_POST['sort'];
                    $data['createtime'] = date('Y-m-d H:i:s');
                    $result = D('Case')->where($where)->save($data);
                    if ($result != false) {
                        $this->redirect('index');
                    } else {
                        $this->error('非法操作!');
                    }
        }else{
            //获取到该条记录的内容
            $list = D('Case')->alias('a')->where('a.id = '.$_GET['id'])->join(' left join ki_case_type as b on a.t_id =b.t_id')->order('a.sort asc')->find();
            $data = D('CaseType')->select();
            if(!$list){
                $this->error('非法操作!');
             }
             if(!data){
                $this->error('非法操作');
             }
                $this->assign('data',$data);
                $this->assign('list',$list);
                $this->display('Caseb/update');
                }
}

    //删除
    public function delete() {
        $imagesModel = M('Case');
        $map['id'] = $_GET['id'];
        $result = $imagesModel->where($map)->delete();
        if ($result) {
            $this->redirect('Caseb/index');
        } else {
            $this->error('删除失败!');
        }
    }
}